package com.hdoit.ares.web.active.wechat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.acti.service.IActiTaskService;
import com.hdoit.ares.logical.acti.service.ITaskGoldenBeanService;
import com.hdoit.ares.logical.active.model.Sign;
import com.hdoit.ares.logical.active.service.ISignService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.enums.acc.GoldenBeanType;
import com.hdoit.ares.selfcommon.enums.acc.HesitatePeriodState;
import com.hdoit.ares.selfcommon.enums.acc.State;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.common.utils.RandomUtils;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping(value="/wechat/active/sign/")
public class SignWechat extends BaseWechat{

	@Override
	public String getRoutePath() {
		return "areas//wechat/active/sign/";
	}
	
	private ISignService signService;	
	private IAccountInfoService accountInfoService;
	private IGoldenBeanDetailService goldenBeanDetailService;
	private IActiTaskService actiTaskService;
	private ITaskGoldenBeanService taskGoldenBeanService;
	
	
	public ITaskGoldenBeanService getTaskGoldenBeanService() {
		return taskGoldenBeanService;
	}
	@Resource
	public void setTaskGoldenBeanService(ITaskGoldenBeanService taskGoldenBeanService) {
		this.taskGoldenBeanService = taskGoldenBeanService;
	}
	public IActiTaskService getActiTaskService() {
		return actiTaskService;
	}
	@Resource
	public void setActiTaskService(IActiTaskService actiTaskService) {
		this.actiTaskService = actiTaskService;
	}
	public ISignService getSignService() {
		return signService;
	}
	@Resource
	public void setSignService(ISignService signService) {
		this.signService = signService;
	}

	public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}
	@Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}

	public IGoldenBeanDetailService getGoldenBeanDetailService() {
		return goldenBeanDetailService;
	}
	@Resource
	public void setGoldenBeanDetailService(
			IGoldenBeanDetailService goldenBeanDetailService) {
		this.goldenBeanDetailService = goldenBeanDetailService;
	}

	private final String signViewName="sign";
	private final String doSignViewName ="doSign";
	
	@RequestMapping(value = signViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/plain;charset=utf-8")
	public String sign(Model model){
		
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);		
		String custId = customer.getId();
				
		Calendar now = Calendar.getInstance();
		String day = now.get(Calendar.DAY_OF_MONTH)+"";
		String  month = (now.get(Calendar.MONTH)+1)+"";	
		
		HashMap<String, Object> val = querySignData(custId, month, day);

		model.addAttribute("signedDate",JsonUtils.toJson(val.get("signedDate")));
		model.addAttribute("isSigned",val.get("isSigned"));
		model.addAttribute("alldays",val.get("alldays"));
		model.addAttribute("continuityDays",val.get("continuityDays"));
		
		//任务引导			
		/*String taskToGuide =  actiTaskService.isDayShare(month, day, customer.getId());		
		model.addAttribute("taskToGuide", taskToGuide);*/	
		
		return view(signViewName);
	}
	
	@RequestMapping(value = doSignViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String doSign(Model model,String signing){
		
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);		
		String custId = customer.getId();
		
		
		@SuppressWarnings("deprecation")
		String month = (DateUtils.getCurrent().getMonth()+1)+"";
		@SuppressWarnings("deprecation")
		String day = (DateUtils.getCurrent().getDate())+"";		
		
		HashMap<String, Object> res = new HashMap<String, Object>();

		//再次确认是否重复签到
		HashMap<String, Object> test = querySignData(custId, month, day);		
		if("true".equals(test.get("isSigned")+"")){
			res.put("isSigned",true);
			return JsonUtils.toJson(res);
		}
		
		if("doSign".equals(signing)){
			
			//数据库添加数据
			Sign sign = new Sign();
			sign.setId(CommonUtils.getKeyId());
			sign.setDeleteMark(YesOrNo.no);
			sign.setCustId(customer.getId());
			sign.setSignDate(DateUtils.getCurrent());
			sign.setSignDays(signService.queryContinuityDays(month,day,custId)+1);
			
			sign.setModelStatus(ModelStatus.add);
			signService.applyChange(sign);		
			
			model.addAttribute("signed","yes");
			res.put("signed", "yes");	
			//金豆或者什么发生改变	(每天 5  7天 10  30天  100)
			
			// 任务临时注释
//			ActiTask actiTask = actiTaskService.queryModelByTaskId(TaskId.sign);
//			int goldenBean = Integer.valueOf(actiTask.getReward());
//			if(StringUtils.isEmpty(actiTask.getReward())){
//				goldenBean=20;
//			}

			int goldenBean = 0;
			if(sign.getSignDays()==7){
				goldenBean = 15;
			}else if (sign.getSignDays()==30) {
				goldenBean = 105;
			}else {
				goldenBean = 5;
			}			
			model.addAttribute("addBean",goldenBean);
			res.put("addBean", goldenBean);
			
			//改变总金豆
			AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());			
			accountInfo.setGoldenBean(accountInfo.getGoldenBean()+goldenBean);
			accountInfo.setModelStatus(ModelStatus.update);
			accountInfoService.applyChange(accountInfo);
			
			//金豆明细
			GoldenBeanDetail goldenBeanDetail = new GoldenBeanDetail();
			goldenBeanDetail.setId(CommonUtils.getKeyId());
			goldenBeanDetail.setAccountNo(accountInfo.getAccountNo());
			goldenBeanDetail.setGoldenBeanType(GoldenBeanType.sign);
			goldenBeanDetail.setTime(DateUtils.getCurrent());
			goldenBeanDetail.setState(State.income);
			goldenBeanDetail.setGoldenBeanAccount(goldenBean);
			goldenBeanDetail.setGoldenBeanBalance(accountInfo.getGoldenBean());
			goldenBeanDetail.setCustomerId(customer.getId());
			goldenBeanDetail.setOutTradeNo("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));
			goldenBeanDetail.setComment("task");
			goldenBeanDetail.setHesitatePeriodState(HesitatePeriodState.Hesitated);
			goldenBeanDetail.setModelStatus(ModelStatus.add);
			goldenBeanDetailService.applyChange(goldenBeanDetail);
			
			// 任务临时注释
//			//向任务统计表里添加数据
//			TaskGoldenBean taskGoldenBean = taskGoldenBeanService.queryModelByCustomerId(customer.getId());
//			taskGoldenBean.setGoldenBean(taskGoldenBean.getGoldenBean()+goldenBean);
//			taskGoldenBeanService.applyChange(taskGoldenBean);
		}
		
		HashMap<String, Object> val = querySignData(custId, month, day);
										
		res.put("signDay", val.get("signedDate"));
		res.put("isSigned", val.get("isSigned"));
		res.put("alldays", val.get("alldays"));
		res.put("continuityDays", val.get("continuityDays"));
		res.put("repeatSign", false);	
			
		return JsonUtils.toJson(res);
	}
	
	public HashMap<String, Object> querySignData(String custId,String month,String day){
		
		HashMap<String, Object> val = new  HashMap<String, Object>();
		//已签到日期
		List<String> signedDates = signService.querySignedDate(month,custId);	
		List<HashMap<String, String>> signedDate =new ArrayList<HashMap<String,String>>();
		for (String stringDate : signedDates) {
			HashMap<String, String> hashMap= new HashMap<String, String>();
			hashMap.put("signDay", stringDate);
			signedDate.add(hashMap);
		}
		//判断当天签到情况
		Boolean isSigned = signService.queryIsSignedToday(month,day,custId);
		//累计签到天数
		Integer alldays = signService.queryAllSignedDays(month,custId);		
		//连续签到天数
		Integer continuityDays =signService.queryContinuityDays(month,day,custId);		
		
		val.put("signedDate", signedDate);
		val.put("isSigned", isSigned);
		val.put("alldays", alldays);
		val.put("continuityDays", continuityDays);
		return val;
	}

}
